import Vue from 'vue'
import VueI18n from 'vue-i18n'
import storage from '@/utils/storage'

Vue.use(VueI18n)

const messages = {
  en: {
    common: {
      confirm: 'Confirm',
      cancel: 'Cancel',
      save: 'Save',
      update: 'Update',
      delete: 'Delete',
      edit: 'Edit',
      create: 'Create',
      refresh: 'Refresh',
      export: 'Export',
      execute: 'Execute',
      connect: 'Connect',
      disconnect: 'Disconnect',
      test: 'Test',
      clear: 'Clear',
      reset: 'Reset',
      apply: 'Apply',
      loading: 'Loading...',
      success: 'Success',
      error: 'Error',
      warning: 'Warning',
      info: 'Info',
      noData: 'No data',
      selectDatabase: 'Select a database',
      selectMeasurement: 'Select a measurement',
      actions: 'Actions'
    },
    sidebar: {
      connections: 'Connections',
      explorer: 'Database Explorer',
      query: 'Query Editor',
      visualization: 'Visualization',
      about: 'About',
      settings: 'Settings'
    },
    connections: {
      title: 'Connection Manager',
      newConnection: 'New Connection',
      editConnection: 'Edit Connection',
      name: 'Name',
      url: 'URL',
      username: 'Username',
      password: 'Password',
      token: 'Token',
      version: 'Version',
      databases: 'Databases',
      status: 'Status',
      active: 'Active',
      inactive: 'Inactive',
      testConnection: 'Test Connection',
      connectionSuccess: 'Connection successful',
      connectionFailed: 'Connection failed',
      deleteConfirm: 'Are you sure you want to delete this connection?',
      duplicateUrl: 'A connection with this URL already exists',
      saveSuccess: 'Connection saved successfully',
      saveFailed: 'Failed to save connection',
      deleteSuccess: 'Connection deleted successfully',
      deleteFailed: 'Failed to delete connection'
    },
    explorer: {
      title: 'Database Explorer',
      databases: 'Databases',
      measurements: 'Measurements',
      measurement: 'Measurement',
      createDatabase: 'Create Database',
      deleteDatabase: 'Delete Database',
      deleteMeasurement: 'Delete Measurement',
      databaseName: 'Database Name',
      searchDatabase: 'Search databases...',
      noMatchingDatabases: 'No matching databases',
      sortBy: 'Sort By',
      sortByNameAsc: 'Name (A-Z)',
      sortByNameDesc: 'Name (Z-A)',
      sortByTimeNewest: 'Latest Data (Newest First)',
      sortByTimeOldest: 'Earliest Data (Oldest First)',
      columns: 'Columns',
      columnSettings: 'Column Settings',
      searchColumns: 'Search columns...',
      noMatchingColumns: 'No matching columns',
      selectAll: 'Select All',
      deselectAll: 'Deselect All',
      reset: 'Reset',
      selectedColumns: 'Selected: {count} / {total}',
      selectAtLeastOneColumn: 'Please select at least one column',
      columnSettingsApplied: 'Column settings applied',
      columnReset: 'Column settings reset',
      timeField: 'Time',
      startTime: 'Start Time',
      endTime: 'End Time',
      limit: 'Limit',
      applyFilter: 'Apply Filter',
      resetFilter: 'Reset Filter',
      exportData: 'Export Data',
      visualize: 'Visualize',
      showingRecords: 'Showing {start} - {end} of {total} records',
      deleteDbConfirm: 'Are you sure you want to delete database "{name}"? This action cannot be undone.',
      deleteMeasurementConfirm: 'Are you sure you want to delete measurement "{name}"? This action cannot be undone.',
      noMeasurementsFound: 'No measurements found in this database'
    },
    query: {
      title: 'Query Editor',
      execute: 'Execute',
      queryHistory: 'Query History',
      clear: 'Clear',
      results: 'Results',
      rows: 'rows',
      exportResults: 'Export Results',
      visualizeResults: 'Visualize Results',
      enterQuery: 'Please enter a query',
      querySuccess: 'Query executed successfully',
      queryFailed: 'Query failed',
      loadQuery: 'Load Query'
    },
    visualization: {
      title: 'Data Visualization',
      chartType: 'Chart Type',
      lineChart: 'Line Chart',
      barChart: 'Bar Chart',
      areaChart: 'Area Chart',
      scatterPlot: 'Scatter Plot',
      xAxis: 'X-Axis',
      yAxis: 'Y-Axis',
      groupBy: 'Group By',
      database: 'Database',
      measurement: 'Measurement',
      records: 'Records',
      exportImage: 'Export Image',
      noDataToVisualize: 'No data to visualize. Please execute a query first.',
      chartTitle: 'Title'
    },
    settings: {
      title: 'Settings',
      language: 'Language',
      languageDescription: 'Select your preferred language',
      english: 'English',
      chinese: '中文',
      saveSuccess: 'Settings saved successfully'
    },
    about: {
      title: 'About',
      version: 'Version',
      tagline: 'A modern, powerful time-series database client',
      description: 'InfluxDB Client is a powerful desktop application built with Electron, Vue.js, and Node.js that provides an intuitive interface for managing and querying InfluxDB databases. This tool simplifies database operations, data visualization, and query management, making it easier for developers and data analysts to work with time-series data.',
      features: 'Key Features',
      connectionManagement: 'Connection Management',
      connectionManagementDesc: 'Manage multiple InfluxDB connections with secure credential storage',
      databaseExplorer: 'Database Explorer',
      databaseExplorerDesc: 'Browse databases, measurements, and data with advanced filtering',
      queryEditor: 'Query Editor',
      queryEditorDesc: 'Write and execute InfluxQL queries with syntax highlighting',
      dataVisualization: 'Data Visualization',
      dataVisualizationDesc: 'Create interactive charts and graphs from query results',
      dataExport: 'Data Export',
      dataExportDesc: 'Export query results in CSV or JSON format',
      queryHistory: 'Query History',
      queryHistoryDesc: 'Access and reuse previously executed queries',
      techStack: 'Technology Stack',
      shortcuts: 'Keyboard Shortcuts',
      support: 'Support & Resources',
      projectGithub: 'Project GitHub',
      projectGitee: 'Project Gitee',
      influxdbDocs: 'InfluxDB Documentation',
      influxdbGithub: 'InfluxDB GitHub',
      communityForum: 'Community Forum',
      officialWebsite: 'Official Website',
      copyright: '© 2025 GuLu. All rights reserved.',
      builtWith: 'Built with ❤️ for the time-series data community'
    }
  },
  zh: {
    common: {
      confirm: '确认',
      cancel: '取消',
      save: '保存',
      update: '更新',
      delete: '删除',
      edit: '编辑',
      create: '创建',
      refresh: '刷新',
      export: '导出',
      execute: '执行',
      connect: '连接',
      disconnect: '断开连接',
      test: '测试',
      clear: '清空',
      reset: '重置',
      apply: '应用',
      loading: '加载中...',
      success: '成功',
      error: '错误',
      warning: '警告',
      info: '信息',
      noData: '暂无数据',
      selectDatabase: '选择数据库',
      selectMeasurement: '选择测量',
      actions: '操作'
    },
    sidebar: {
      connections: '连接管理',
      explorer: '数据库浏览器',
      query: '查询编辑器',
      visualization: '数据可视化',
      about: '关于',
      settings: '设置'
    },
    connections: {
      title: '连接管理器',
      newConnection: '新建连接',
      editConnection: '编辑连接',
      name: '名称',
      url: 'URL',
      username: '用户名',
      password: '密码',
      token: '令牌',
      version: '版本',
      databases: '数据库',
      status: '状态',
      active: '已连接',
      inactive: '未连接',
      testConnection: '测试连接',
      connectionSuccess: '连接成功',
      connectionFailed: '连接失败',
      deleteConfirm: '确定要删除此连接吗？',
      duplicateUrl: '已存在相同地址的连接',
      saveSuccess: '连接保存成功',
      saveFailed: '连接保存失败',
      deleteSuccess: '连接删除成功',
      deleteFailed: '连接删除失败'
    },
    explorer: {
      title: '数据库浏览器',
      databases: '数据库',
      measurements: '测量',
      measurement: '测量',
      createDatabase: '创建数据库',
      deleteDatabase: '删除数据库',
      deleteMeasurement: '删除测量',
      databaseName: '数据库名称',
      searchDatabase: '搜索数据库...',
      noMatchingDatabases: '没有匹配的数据库',
      sortBy: '排序方式',
      sortByNameAsc: '名称 (A-Z)',
      sortByNameDesc: '名称 (Z-A)',
      sortByTimeNewest: '最新数据 (新到旧)',
      sortByTimeOldest: '最早数据 (旧到新)',
      columns: '列',
      columnSettings: '列设置',
      searchColumns: '搜索列...',
      noMatchingColumns: '没有匹配的列',
      selectAll: '全选',
      deselectAll: '全不选',
      reset: '重置',
      selectedColumns: '已选: {count} / {total}',
      selectAtLeastOneColumn: '请至少选择一列',
      columnSettingsApplied: '列设置已应用',
      columnReset: '列设置已重置',
      timeField: '时间字段',
      startTime: '开始时间',
      endTime: '结束时间',
      limit: '限制',
      applyFilter: '应用筛选',
      resetFilter: '重置筛选',
      exportData: '导出数据',
      visualize: '可视化',
      showingRecords: '显示 {start} - {end}，共 {total} 条记录',
      deleteDbConfirm: '确定要删除数据库 "{name}" 吗？此操作不可撤销。',
      deleteMeasurementConfirm: '确定要删除测量 "{name}" 吗？此操作不可撤销。',
      noMeasurementsFound: '此数据库中没有找到测量'
    },
    query: {
      title: '查询编辑器',
      execute: '执行',
      queryHistory: '查询历史',
      clear: '清空',
      results: '结果',
      rows: '行',
      exportResults: '导出结果',
      visualizeResults: '可视化结果',
      enterQuery: '请输入查询语句',
      querySuccess: '查询执行成功',
      queryFailed: '查询失败',
      loadQuery: '加载查询'
    },
    visualization: {
      title: '数据可视化',
      chartType: '图表类型',
      lineChart: '折线图',
      barChart: '柱状图',
      areaChart: '面积图',
      scatterPlot: '散点图',
      xAxis: 'X轴',
      yAxis: 'Y轴',
      groupBy: '分组',
      database: '数据库',
      measurement: '测量',
      records: '记录',
      exportImage: '导出图片',
      noDataToVisualize: '没有可视化的数据，请先执行查询。',
      chartTitle: '标题'
    },
    settings: {
      title: '设置',
      language: '语言',
      languageDescription: '选择您的首选语言',
      english: 'English',
      chinese: '中文',
      saveSuccess: '设置保存成功'
    },
    about: {
      title: '关于',
      version: '版本',
      tagline: '现代化、强大的时序数据库客户端',
      description: 'InfluxDB Client 是一款基于 Electron、Vue.js 和 Node.js 构建的功能强大的桌面应用程序，为管理和查询 InfluxDB 数据库提供了直观的界面。此工具简化了数据库操作、数据可视化和查询管理，使开发人员和数据分析师更容易处理时间序列数据。',
      features: '主要功能',
      connectionManagement: '连接管理',
      connectionManagementDesc: '管理多个 InfluxDB 连接，安全存储凭据',
      databaseExplorer: '数据库浏览器',
      databaseExplorerDesc: '使用高级筛选功能浏览数据库、测量和数据',
      queryEditor: '查询编辑器',
      queryEditorDesc: '编写和执行带语法高亮的 InfluxQL 查询',
      dataVisualization: '数据可视化',
      dataVisualizationDesc: '从查询结果创建交互式图表和图形',
      dataExport: '数据导出',
      dataExportDesc: '以 CSV 或 JSON 格式导出查询结果',
      queryHistory: '查询历史',
      queryHistoryDesc: '访问和重用以前执行的查询',
      techStack: '技术栈',
      shortcuts: '快捷键',
      support: '支持与资源',
      projectGithub: '项目 GitHub',
      projectGitee: '项目 Gitee',
      influxdbDocs: 'InfluxDB 文档',
      influxdbGithub: 'InfluxDB GitHub',
      communityForum: '社区论坛',
      officialWebsite: '官方网站',
      copyright: '© 2025 GuLu. 保留所有权利。',
      builtWith: '为时间序列数据社区用 ❤️ 打造'
    }
  }
}

// Load saved language preference or use default
const savedLanguage = storage.loadSettings()?.language || 'en'

const i18n = new VueI18n({
  locale: savedLanguage,
  fallbackLocale: 'en',
  messages
})

export default i18n